package com.android.openapp.ui;

import android.app.Dialog;
import android.content.Context;
import android.util.TypedValue;
import android.view.View.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

/**
 * 
 * @author Simone Panza
 *	date 2012_08_22
 *	version 1.0
 *
 *	Dialog that shows a series of text screens and a "next" button to navigate through them.
 *	Useful to show a tutorial of your app.
 */

public class TutorialDialog extends Dialog implements OnClickListener, ViewSwitcher.ViewFactory {

	private TextSwitcher mTS;
	private Button but;
	private Context mContext;
	private int counter = 0;
	private String[] str;
	
	/**
	 * 
	 * @param context the Activity from which the Dialog is created
	 * @param screenId the layout which will be displayed into the Dialog
	 * @param textSwitcherId the View associated with the screenId must contain a TextSwitcher
	 */
	public TutorialDialog(Context context, int screenId, int textSwitcherId){
		super(context);
		mContext = context;
		
		setCancelable(false);
		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		
		//inflate the layout
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(screenId, null);
		
        mTS = (TextSwitcher) layout.findViewById(textSwitcherId);
        but = (Button) layout.findViewById(android.R.id.button1);
        
        but.setOnClickListener(this);
        mTS.setFactory(this);
        
        setContentView(layout);
	}
	
	public void showWithIcon(){
		show();
		setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.ic_dialog_info);
	}
	
	public void setTextStrings(String[] s){
		str = s;
		if(str != null && str.length > 0){
			refreshButtonText();
			mTS.setText(str[0]);
			counter = 0;
		}
	}

	private void refreshButtonText(){
		if(counter == str.length - 1)
        	but.setText("OK!");
        else
        	but.setText("Next");
	}
	
	/**
	 * Callback method used by TextSwitcher for switching between one TextView and another
	 */
	@Override
	public View makeView() {
		TextView tv = new TextView(mContext);
		tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
		return tv;
	}

	@Override
	public void onClick(View v) {
		counter++;
		if(counter < str.length){
	        refreshButtonText();
	        
			mTS.setText(str[counter]);
			
		}
		else
			cancel();
	}
}
